Source for file vcard.class.php

Documentation is available at vcard.class.php

  1. <?php
  2. /***************************************************************************
  3.  
  4. PHP vCard class v2.0
  5. (c) Kai Blankenhorn
  6. www.bitfolge.de/en
  7. kaib@bitfolge.de
  8.  
  9.  
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  23.  
  24. ***************************************************************************/
  25.  
  26.  
  27. function encode($string{
  28.     return escape(quoted_printable_encode($string));
  29. }
  30.  
  31. function escape($string{
  32.     return str_replace(";","\;",$string);
  33. }
  34.  
  35. // taken from PHP documentation comments
  36. function quoted_printable_encode($input$line_max 76{
  37.     $hex array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  38.     $lines preg_split("/(?:\r\n|\r|\n)/"$input);
  39.     $eol "\r\n";
  40.     $linebreak "=0D=0A";
  41.     $escape "=";
  42.     $output "";
  43.  
  44.     for ($j=0;$j<count($lines);$j++{
  45.         $line $lines[$j];
  46.         $linlen strlen($line);
  47.         $newline "";
  48.         for($i 0$i $linlen$i++{
  49.             $c substr($line$i1);
  50.             $dec ord($c);
  51.             if ( ($dec == 32&& ($i == ($linlen 1)) ) // convert space at eol only
  52.                 $c "=20"
  53.             elseif ( ($dec == 61|| ($dec 32 || ($dec 126) ) // always encode "\t", which is *not* required
  54.                 $h2 floor($dec/16)$h1 floor($dec%16)
  55.                 $c $escape.$hex["$h2"].$hex["$h1"]
  56.             }
  57.             if ( (strlen($newlinestrlen($c)) >= $line_max // CRLF is not counted
  58.                 $output .= $newline.$escape.$eol// soft line break; " =\r\n" is okay
  59.                 $newline "    ";
  60.             }
  61.             $newline .= $c;
  62.         // end of for
  63.         $output .= $newline;
  64.         if ($j<count($lines)-1$output .= $linebreak;
  65.     }
  66.     return trim($output);
  67. }
  68.  
  69. class vCard {
  70.     var $properties;
  71.     var $filename;
  72.     
  73.     function setPhoneNumber($number$type=""{
  74.     // type may be PREF | WORK | HOME | VOICE | FAX | MSG | CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO or any senseful combination, e.g. "PREF;WORK;VOICE"
  75.         $key "TEL";
  76.         if ($type!=""$key .= ";".$type;
  77.         $key.= ";ENCODING=QUOTED-PRINTABLE";
  78.         $this->properties[$keyquoted_printable_encode($number);
  79.     }
  80.     
  81.     // UNTESTED !!!
  82.     function setPhoto($type$photo// $type = "GIF" | "JPEG"
  83.         $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"base64_encode($photo);
  84.     }
  85.     
  86.     function setFormattedName($name{
  87.         $this->properties["FN"quoted_printable_encode($name);
  88.     }
  89.     
  90.     function setName($family=""$first=""$additional=""$prefix=""$suffix=""{
  91.         $this->properties["N""$family;$first;$additional;$prefix;$suffix";
  92.         $this->filename = "$first%20$family.vcf";
  93.         if ($this->properties["FN"]==""$this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
  94.     }
  95.     
  96.     function setBirthday($date// $date format is YYYY-MM-DD
  97.         $this->properties["BDAY"$date;
  98.     }
  99.     
  100.     function setAddress($postoffice=""$extended=""$street=""$city=""$region=""$zip=""$country=""$type="HOME;POSTAL"{
  101.     // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
  102.         $key "ADR";
  103.         if ($type!=""$key.= ";$type";
  104.         $key.= ";ENCODING=QUOTED-PRINTABLE";
  105.         $this->properties[$keyencode($name).";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
  106.         
  107.         if ($this->properties["LABEL;$type;ENCODING=QUOTED-PRINTABLE"== ""{
  108.             //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
  109.         }
  110.     }
  111.     
  112.     function setLabel($postoffice=""$extended=""$street=""$city=""$region=""$zip=""$country=""$type="HOME;POSTAL"{
  113.         $label "";
  114.         if ($postoffice!=""$label.= "$postoffice\r\n";
  115.         if ($extended!=""$label.= "$extended\r\n";
  116.         if ($street!=""$label.= "$street\r\n";
  117.         if ($zip!=""$label.= "$zip ";
  118.         if ($city!=""$label.= "$city\r\n";
  119.         if ($region!=""$label.= "$region\r\n";
  120.         if ($country!=""$country.= "$country\r\n";
  121.         
  122.         $this->properties["LABEL;$type;ENCODING=QUOTED-PRINTABLE"quoted_printable_encode($label);
  123.     }
  124.     
  125.     function setEmail($address{
  126.         $this->properties["EMAIL;INTERNET"$address;
  127.     }
  128.     
  129.     function setNote($note{
  130.         $this->properties["NOTE;ENCODING=QUOTED-PRINTABLE"quoted_printable_encode($note);
  131.     }
  132.     
  133.     function setURL($url$type=""{
  134.     // $type may be WORK | HOME
  135.         $key "URL";
  136.         if ($type!=""$key.= ";$type";
  137.         $this->properties[$key$url;
  138.     }
  139.     
  140.     function getVCard({
  141.         $text "BEGIN:VCARD\r\n";
  142.         $text.= "VERSION:2.1\r\n";
  143.         foreach($this->properties as $key => $value{
  144.             $text.= "$key:$value\r\n";
  145.         }
  146.         $text.= "REV:".date("Y-m-d")."T".date("H:i:s")."Z\r\n";
  147.         $text.= "MAILER:PHP vCard class by Kai Blankenhorn\r\n";
  148.         $text.= "END:VCARD\r\n";
  149.         return $text;
  150.     }
  151.     
  152.     function getFileName({
  153.         return $this->filename;
  154.     }
  155. }
  156.  
  157.  
  158. //  USAGE EXAMPLE
  159. /*
  160. $v = new vCard();
  161.  
  162. $v->setPhoneNumber("+49 23 456789", "PREF;HOME;VOICE");
  163. $v->setName("Mustermann", "Thomas", "", "Herr");
  164. $v->setBirthday("1960-07-31");
  165. $v->setAddress("", "", "Musterstrasse 20", "Musterstadt", "", "98765", "Deutschland");
  166. $v->setEmail("thomas.mustermann@thomas-mustermann.de");
  167. $v->setNote("You can take some notes here.\r\nMultiple lines are supported via \\r\\n.");
  168. $v->setURL("http://www.thomas-mustermann.de", "WORK");
  169.  
  170. $output = $v->getVCard();
  171. $filename = $v->getFileName();
  172.  
  173. Header("Content-Disposition: attachment; filename=$filename");
  174. Header("Content-Length: ".strlen($output));
  175. Header("Connection: close");
  176. Header("Content-Type: text/x-vCard; name=$filename");
  177.  
  178. echo $output;
  179. */
  180. ?>

Documentation generated on Mon, 05 May 2008 16:24:05 +0400 by phpDocumentor 1.4.0